package org.come.Jpanel;

import com.tool.image.ImageMixDeal;
import com.tool.role.RoleData;
import com.tool.tcp.NewPart;
import com.tool.tcp.PartTwo;
import com.tool.tcp.Sprite;
import com.tool.tcp.SpriteFactory;
import com.tool.tcpimg.RichLabel;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;
import org.come.bean.ConfigureBean;
import org.come.bean.LoginResult;
import org.come.model.Configure;
import org.come.tt.Ladder;
import org.come.tt.LadderPanelBtn;
import org.come.until.SrcollPanelUI;
import org.come.until.UserMessUntil;

import javax.swing.*;
import java.awt.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public class LadderJpanel1 extends JPanel {

    // 背景图片
    private ImageIcon beijing;

    // 头像图片
    private JLabel headImg;

    private LadderPanelBtn startTheGame, ladderPoints, ladderLottery, award1, award2, award3;

    private String seasonInfo = "";
    private String currSeason = "";
    private JScrollPane jscPanel;
    private RichLabel richText;

    private List<LoginResult> loginResultList;

    public String getSeasonInfo() {
        return seasonInfo;
    }

    public void setSeasonInfo(String seasonInfo) {
        this.seasonInfo = seasonInfo;
    }

    public String getCurrSeason() {
        return currSeason;
    }

    public void setCurrSeason(String currSeason) {
        this.currSeason = currSeason;
    }

    public List<LoginResult> getLoginResultList() {
        return loginResultList;
    }

    public void setLoginResultList(List<LoginResult> loginResultList) {
        this.loginResultList = loginResultList;
    }

    static Sprite tcp = SpriteFactory.VloadSprite("resource/FightingSkill/持续状态/tx1.tcp", null);//特效路径
    static Sprite tcp2 = SpriteFactory.VloadSprite("resource/FightingSkill/持续状态/tx2.tcp", null);//特效路径
    private ImageIcon icon = null;
    static {
        if (tcp == null) {
            tcp = tcp;

        }
    }
    static {
        if (tcp2 == null) {
            tcp2 = tcp2;

        }
    }
    public LadderJpanel1() {
        this.setPreferredSize(new Dimension(614, 400));
        this.setLayout(null);
        this.setBackground(UIUtils.Color_BACK);
        getJscPanel();
        if("水墨UI".equals(MyIsif.getStyle())) {
            beijing = new ImageIcon("inkImg/newbackground/B301.png");
        }else{
            beijing = new ImageIcon("inkImg/newbackground/B301.png");
        }
//人物头像暂时屏蔽
//        headImg = new JLabel();
//        headImg.setBounds(30, 50, 75, 100);
//        ImageIcon Img = CutButtonImage.getImage("img/head/" + RoleData.getRoleData().getLoginResult().getSpecies_id() + ".png", 75, 100);
//        headImg.setIcon(Img);
//        this.add(headImg);


        //开始比赛
        startTheGame = new LadderPanelBtn("skin/8100226/newbutton/B35.png", 1, 10);
        startTheGame.setBounds(481, 303, 60, 60);
        this.add(startTheGame);
        //天梯积分
        ladderPoints = new LadderPanelBtn("skin/8100226/button/ttjf.png", 1, 2);
//        ladderPoints.setBounds(550, 75, 100, 57);
        ladderPoints.setBounds(248, 170, 100, 22);
        this.add(ladderPoints);

//        //天梯抽奖
//        ladderLottery = new LadderPanelBtn("skin/8100226/button/ttjc.png", 1, 3);
//        ladderLottery.setBounds(550, 105, 100, 57);
//        this.add(ladderLottery);
//        //奖励1
//        award1 = new LadderPanelBtn("skin/8100226/button/ttcc1.png", 1, 4);
//        award1.setBounds(450, 40, 80, 62);
//        this.add(award1);
//        //奖励2
//        award2 = new LadderPanelBtn("skin/8100226/button/ttcc10.png", 1, 5);
//        award2.setBounds(450, 70, 80, 62);
//        this.add(award2);
//        //奖励3
//        award3 = new LadderPanelBtn("skin/8100226/button/ttcc20.png", 1, 6);
//        award3.setBounds(450, 100, 80, 62);
//        this.add(award3);



//        // 关闭按钮事件
//        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 604);
//        offBtn.setBounds(684 - 37, 10, 25, 25);
//        this.add(offBtn);
    }

    @Override
    protected void paintComponent(Graphics g) {

        ConfigureBean allConfigure = UserMessUntil.getConfigureBean();
        Map<BigDecimal, Configure> item = allConfigure.getAllConfigure();
        Configure configure = item.get(new BigDecimal(1));
        String nao = "新";
        if(configure.getNeworold()!=null) {
            nao = configure.getNeworold();
        }

        // TODO Auto-generated method stub
        super.paintComponent(g);
        g.drawImage(beijing.getImage(), 0, 5, 614, 345, null);
        LoginResult login = RoleData.getRoleData().getLoginResult();
        BigDecimal ttRecord = login.getScoretype("天梯积分");
        String userName = login.getRolename();
        g.setFont(UIUtils.TEXT_BOLD_FONT);


        if (loginResultList != null) {
            int x = loginResultList.size();
            if(x>5) {
                x = 5;
            }
            g.setFont(UIUtils.TEXT_FONT);
            for (int i = 0; i < x; i++) {

                LoginResult loginResult = loginResultList.get(i);
                //                天梯点击卡死（显示名字就不正常暂时未解决）
                int cout = loginResult.getTtFail() + loginResult.getFighting();
                if (cout == 0) {
                    continue;   //teamArena
                }
                //                天梯点击卡死（显示名字就不正常暂时未解决）

                g.setColor(Color.black);

                String skin = loginResult.getSpecies_id().toString();
                if(!"新".equals(nao)) {
                    NewPart partOne = SpriteFactory.createPart(skin, 2, 1, null);
                    partOne.draw(g, 100 + i * 120, 270, 4, ImageMixDeal.userimg.getTime());
                }else {
                    PartTwo partTwo = (PartTwo) SpriteFactory.createPart(skin, 2, 1, null);
                    partTwo.draw(g, 100 + i * 120, 270, 4, ImageMixDeal.userimg.getTime());
                }
                Ladder ladder = getLadder(loginResult.getGrade());
                g.setColor(ladder.getColor());
//                天梯点击卡死（显示名字就不正常暂时未解决）
                g.drawString(loginResult.getRolename(), 95 + i * 115, 325);
//                天梯点击卡死（显示名字就不正常暂时未解决）
                g.drawString(ladder.getLevel(), 95 + i * 115, 342);
                g.setColor(Color.black);
                g.drawString("胜" + loginResult.getTtVictory() + "/败" + loginResult.getTtFail(), 95 + i * 115, 380);
                g.drawString(loginResult.getScoretype("天梯积分").toString(), 95 + i * 115, 360);
            }
        }

        Boolean b = false;
        Integer index = 0;
        if (loginResultList != null) {
            for (LoginResult loginResult : loginResultList) {
                index += 1;
                if (loginResult.getRole_id().compareTo(login.getRole_id()) == 0) {
                    b = true;
                    break;
                }
            }
        }

        Ladder ladder = getLadder(login.getGrade());

//        g.setColor(ladder.getColor());
        if("水墨UI".equals(MyIsif.getStyle())) {
            g.setColor(Color.black);
        }else{
            g.setColor(Color.orange);
        }
//        g.drawString(userName, 180, 30);
//        g.drawString(ladder.getLevel(), 180, 60);
        g.drawString(ttRecord.intValue() + "", 200, 250);
        g.drawString(b ? "第 " + index + " 名" : "暂未上榜", 200, 296);
        g.drawString("胜" + login.getTtVictory() + "/败" + login.getTtFail(), 250, 340);
//        g.drawString("暂无排名", 350, 136);


        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setFont(UIUtils.TEXT_HYK20);
        if(this.currSeason!=null) {
            g2d.drawString(this.currSeason, 50, 40);
            g2d.setFont(UIUtils.TEXT_NAME_FONT);
            g2d.setColor(Color.red);
            g2d.drawString(this.seasonInfo, 50, 170);
        }

    }

    private Ladder getLadder(Integer grade) {
        Ladder ladder = new Ladder();
        // 判断人物的等级
        if (grade <= 102) {
            ladder.setColor(new Color(0, 255, 0));
            ladder.setLevel("0转" + grade + "级");
        } else if (grade > 102 && grade <= 210) {
            ladder.setColor(new Color(255, 140, 0));
            ladder.setLevel("1转" + (grade - 102 + 14) + "级");
        } else if (grade > 210 && grade <= 338) {
            ladder.setColor(new Color(0, 245, 255));
            ladder.setLevel("2转" + (grade - 210 + 14) + "级");
        } else if (grade > 338 && grade <= 459) {
            ladder.setColor(new Color(238, 44, 44));
            ladder.setLevel("3转" + (grade - 338 + 59) + "级");
        } else if (grade > 459) {
            ladder.setColor(new Color(155, 48, 255));
            ladder.setLevel("飞升" + (grade - 459 + 139) + "级");
        }
        return ladder;
    }

    public JScrollPane getJscPanel() {
        if (jscPanel == null) {
            // 属性滚动条
            jscPanel = new JScrollPane(getRichText());
            jscPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            jscPanel.getVerticalScrollBar().setUI(new SrcollPanelUI());
            jscPanel.getVerticalScrollBar().setUnitIncrement(20);
            jscPanel.getViewport().setOpaque(false);
            jscPanel.setOpaque(false);
            if("水墨UI".equals(MyIsif.getStyle())) {
                jscPanel.setBounds(430, 88, 178, 190);
            }else{
                jscPanel.setBounds(432, 88, 170, 188);
            }
            jscPanel.setBorder(BorderFactory.createEmptyBorder());
            jscPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            this.add(jscPanel);
        }

        return jscPanel;
    }

    public void setJscPanel(JScrollPane jscPanel) {
        this.jscPanel = jscPanel;
    }

    public RichLabel getRichText() {
        if(richText == null){
            richText = new RichLabel();
            richText.setText("试炼规则：#r1,累计挑战奖励机制#r2,原则可以无限挑战#r3,成功挑战后解锁新关卡#r4,挑战新关卡不消耗挑战次数#r5,每层的奖励唯一，可以选择性击杀");
            Dimension d = richText.computeSize(151);
            richText.setSize(d);
            richText.setPreferredSize(d);
        }
        return richText;
    }

    public void setRichText(RichLabel richText) {
        this.richText = richText;
    }
    //新增加tcp
    @Override
    public void paint(Graphics g) {

        tcp.updateToTime(ImageMixDeal.userimg.getTime(), 0);
        tcp2.updateToTime(ImageMixDeal.userimg.getTime(), 0);
        super.paint(g);
        tcp.draw(g, 510, 335);
        tcp2.draw(g, 200, 80);

    }
}
